package com.shelpe.services.sinbadgateway.client;

import java.util.ArrayList;
import java.util.List;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.shelpe.services.sinbadgateway.client.LexiconClient.LexiconClientFallbackFallback;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

@FeignClient(name = "${feign.lexiconserver.name}", url = "${feign.lexiconserver.url}",fallback = LexiconClientFallbackFallback.class )
public interface LexiconClient {

	@RequestMapping(method = RequestMethod.GET, path = "/lexiconservice/adgroups/{adgroup_id}/recommends", consumes = "application/json;charset=UTF-8")
	List<RecommendWordDto> smartWords(@PathVariable("adgroup_id") long adgroupID,
			@RequestParam(name = "mode") int modeValue, @RequestParam(name = "defaultPrice") float defaultPrice);

	@RequestMapping(method = RequestMethod.GET, path = "/lexiconservice/adgroups/{adgroup_id}/recommends", consumes = "application/json;charset=UTF-8")
	List<RecommendWordDto> smartWordsAsDevice(@PathVariable("adgroup_id") long adgroupID,
			@RequestParam(name = "device") int deviceValue);

	/*
	 * @RequestMapping(method = RequestMethod.POST,
	 * path="/lexiconservice/adgroups/{adgroup_id}/records",
	 * consumes="application/json;charset=UTF-8") Response
	 * createAdgroupRecord(@PathVariable("adgroup_id")long
	 * adgroupID, @RequestBody AdgroupRecordDto dto);
	 */

	@RequestMapping(method = RequestMethod.POST, path = "/lexiconservice/adgroups/{adgroup_id}/records", consumes = "application/json;charset=UTF-8")
	Response createAdgroupRecord(@PathVariable("adgroup_id") long adgroupID, @RequestBody AdgroupRecordDto dto);

	@RequestMapping(method = RequestMethod.POST, path = "/lexiconservice/adgroups/{adgroup_id}/metrics", consumes = "application/json;charset=UTF-8")
	Response syncAdgroupRecordMetrics(@PathVariable("adgroup_id") long adgroupID);

	@RequestMapping(method = RequestMethod.POST, path = "/lexiconservice/adgroups/{adgroup_id}/tags", consumes = "application/json;charset=UTF-8")
	Response calcAdgroupRecordTags(@PathVariable("adgroup_id") long adgroupID);

	@RequestMapping(method = RequestMethod.POST, path = "/lexiconservice/adgroups/{adgroup_id}/metrics", consumes = "application/json;charset=UTF-8")
	Response syncAdgroupRecordMetrics(@PathVariable("adgroup_id") long adgroupID, @RequestBody AdgroupMetricDto dto);

	// 大盘
	@Data
	public static class AdgroupMetricDto {
		@JsonProperty("sync_category")
		private boolean syncCategory = true;
		@JsonProperty("sync_device")
		private boolean syncDevice = true;
	}

	@Data
	public static class AdgroupRecordDto {
		private boolean needSync;

		public AdgroupRecordDto() {
		}

		public AdgroupRecordDto(boolean needSync) {
			this.needSync = needSync;
		}
	}

	@Data
	public static class Response {
		private long adgroupID;
		private String adgroupRecordID;
		private boolean status;
		private SimbaProxyBaseDto.Error error;
	}

	@Data
	public static class RecommendWordDto {
		private String word;

		private int pertinence;// 相关性
		private int competition;// 竞争度
		private int impression;// 展现量

		@JsonProperty("pv")
		private int pageVolume;
		@JsonProperty("avg_price")
		private float averagePrice;// 平均价格
		private float price;

		private int click;// 点击量
		@JsonProperty("ctr")
		private float clickRatio;// 点击率
		private float clickCoverage;// 点击转化率

		private boolean centeral;
	}

	@Service
	@Slf4j
	public static class LexiconClientFallbackFallback implements LexiconClient {
		@Override
		public List<RecommendWordDto> smartWords(long adgroupID, int modeValue, float defaultPrice) {
			log.error("to smartWords fallback: {}, {}", adgroupID, defaultPrice);
			return new ArrayList<RecommendWordDto>();
		}

		@Override
		public List<RecommendWordDto> smartWordsAsDevice(long adgroupID, int deviceValue) {
			log.error("to smartWordsAsDevice fallback: {}, {}", adgroupID, deviceValue);
			return new ArrayList<RecommendWordDto>();
		}

		@Override
		public Response createAdgroupRecord(long adgroupID, AdgroupRecordDto dto) {
			log.error("to createAdgroupRecord fallback: {}, {}", adgroupID, dto);
			return new Response();
		}

		@Override
		public Response syncAdgroupRecordMetrics(long adgroupID) {
			log.error("to syncAdgroupRecordMetrics fallback: {}", adgroupID);
			return new Response();
		}

		@Override
		public Response calcAdgroupRecordTags(long adgroupID) {
			log.error("to calcAdgroupRecordTags fallback: {}", adgroupID);
			return new Response();
		}

		@Override
		public Response syncAdgroupRecordMetrics(long adgroupID, AdgroupMetricDto dto) {
			log.error("to syncAdgroupRecordMetrics fallback: {}, {}", adgroupID, dto);
			return new Response();
		}

	}

}
